package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tccb.ebank.TccbB2cMessage;

@Controller
@Scope("prototype")
public class TccbB2cServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(TccbB2cServerRes.class);

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		TccbB2cMessage message = new TccbB2cMessage();
		String plain = request.getParameter("Plain");// 明文
		String signature = request.getParameter("Signature");// 密文
		message.setPlainMessage(plain);
		message.setSignMessage(signature);
		message.setBank(BankConstants.BANK_LOGO_TCCB);
		parsePlain2Message(message);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		TccbB2cMessage message = (TccbB2cMessage) result.getModel("message");
		orderInfo.setOrderId(message.getOrderId());
		orderInfo.setAmt(String.valueOf(message.getAmount().longValue()));
		orderInfo.setNotifyNum("1");
		orderInfo.setPayStatus("");
		orderInfo.setRetCode("00");
		LinkedHashMap<String, String> map = parsePlain4Map(message.getPlainMessage());
		StringBuffer respStr = new StringBuffer("Plain=");
		respStr.append("MerchantId=").append(map.get("MerchantId")).append("~|~");
		respStr.append("MerchantSeqNo=").append(map.get("MerchantSeqNo")).append("~|~");
		respStr.append("MerchantDateTime=").append(map.get("MercDtTm")).append("~|~");
		respStr.append("Status=").append(map.get("1")).append("~|~");
		respStr.append("MerchantUrl2=").append("");
		orderInfo.setRetMsg(respStr.toString());
		return orderInfo;
	}

	@RequestMapping(value = "tccbB2cServerRes")
	@Override
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- TccbB2cServerRes bankback ---------------");
		return super.execute(request, response);
	}

	/**
	 * 解析明文放入Map.
	 * 
	 * @param dataStr
	 * @return
	 */
	private LinkedHashMap<String, String> parsePlain4Map(String dataStr) {
		if (dataStr == null || dataStr.length() == 0) {
			return null;
		}
		LinkedHashMap<String, String> retMap = new LinkedHashMap<String, String>();
		String terms[] = dataStr.split("~\\|~");
		int len = terms.length;
		for (int i = 0; i < len; i++) {
			String term = terms[i];
			int index = term.indexOf("=");
			retMap.put(term.substring(0, index), term.substring(index + 1));
		}
		return retMap;
	}

	private void parsePlain2Message(TccbB2cMessage message) {
		LinkedHashMap<String, String> plainMap = parsePlain4Map(message.getPlainMessage());
		message.setTransName(plainMap.get("TransName"));
		message.setAcqSsn(plainMap.get("AcqSsn"));
		message.setMercDtTm(plainMap.get("MercDtTm"));
		message.setMerchantSeqNo(plainMap.get("MerchantSeqNo"));
		message.setRespCode(plainMap.get("RespCode"));
		message.setTermCode(plainMap.get("TermCode"));
		message.setMerchantId(plainMap.get("MerchantId"));
		message.setTranAmt(plainMap.get("TranAmt"));
		message.setAmount(new BigDecimal(plainMap.get("TranAmt")));
		message.setSettDate(plainMap.get("SettDate"));
		message.setBankOrderId(plainMap.get("MerchantSeqNo"));
		message.setOrderId(plainMap.get("MerchantSeqNo"));
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		String plain = request.getParameter("Plain");// 明文
		String signature = request.getParameter("Signature");// 密文		
		if (StringUtils.isEmpty(plain) || StringUtils.isEmpty(signature)) {
		return false;
		}
		return true;
	}
}
